package com.saifan.wyy_ov.ui.home;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.MaterialDialog;
import com.saifan.wyy_ov.utils.DialogUtil;
import com.saifan.wyy_ov.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-6-18 21:06:00
 * @descrption
 */

public class FeedbackActivity extends BaseActivity implements TextWatcher {
    private android.support.v7.widget.Toolbar toolbar;
    private android.widget.EditText content;
    private android.widget.TextView numberTip;
    private android.widget.EditText contacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();

    }

    public void setContentView() {
        setContentView(R.layout.activity_feedback);
        this.contacts = (EditText) findViewById(R.id.contacts);
        this.numberTip = (TextView) findViewById(R.id.numberTip);
        this.content = (EditText) findViewById(R.id.context);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        content.addTextChangedListener(this);
    }

    public void submit(View view) {
        if (isEmpty(content)) {
            ToastUtil.showToast(this, "反馈内容不能为空 ");
            return;
        }
        if (isEmpty(contacts)) {
            ToastUtil.showToast(this, "请留下联系方式！");
            return;
        }

        try {
            // 意见反馈参数{"Content":"","YXDID":"","FeedbackPeople":"","VersionType":"2","Contact":""}
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("YXDID", getRoomInfoBean().getXMBS());
            jsonObject.put("FeedbackPeople", getRoomInfoBean().getWYKHDA_KHMC());
            jsonObject.put("Content", content.getText().toString().trim());
            jsonObject.put("VersionType", 2);
            jsonObject.put("Contact", contacts.getText().toString().trim());
            new GetData().getDataJson(this, Api.Customer.FEEDBACK_SUBMIT, jsonObject, getString(R.string.wait), new ResponseData() {

                @Override
                public void onSuccess(String json) {
                    final MaterialDialog dialog = DialogUtil.createDailog(FeedbackActivity.this, "反馈成功！", "非常感谢您的宝贵意见！");
                    dialog.show();
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            //execute the task
                            dialog.dismiss();
                            finish();
                        }
                    }, 3000);


                }

                @Override
                public void onFailed(String str) {

                }
            });
        } catch (JSONException e) {

        }

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        numberTip.setText(s.length() + "/180");

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
